import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)

const routes = [
    // {
    //   path: '/',
    //   name: 'home',
    //   component: HomeView
    // },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
    // }
    // 首页 登录
    {
        path: "/index",
        name: "index",
        component: resolve => require(['@views/index'], resolve),
        meta: {title:'少女虫拍客空间',keepAlive: false},
    },
    // 主题
    {
        path: "/theme",
        name: "theme",
        component: resolve => require(['@views/theme'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间', keepAlive: true},
    },
    // 商品
    {
        path: "/goodList",
        name: "goodList",
        component: resolve => require(['@views/goodList'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    },    
    // 方式选择  
    {
        path: "/modeSelect",
        name: "modeSelect",
        component: resolve => require(['@views/modeSelect'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    }, 
    // 上传照片
    {
        path: "/uploadPhotos",
        name: "uploadPhotos",
        component: resolve => require(['@views/uploadPhotos'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    }, 
    // 拍照页面
    {
        path: "/photograph",
        name: "photograph",
        component: resolve => require(['@views/photograph'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    },
    // 其他操作页面
    {
        path: "/otherList",
        name: "otherList",
        component: resolve => require(['@views/otherList'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    },
    // 广告
    {
        path: "/",
        name: "advertisement",
        component: resolve => require(['@views/advertisement'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    },    
    // 公众号(下载页面)
    {
        path: "/download",
        name: "download",
        component: resolve => require(['@views/download'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    },  
    // 打印页面
    {
        path: "/printPage",
        name: "printPage",
        component: resolve => require(['@views/printPage'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    },
    // 错误页面
    {
        path: "/error",
        name: "error",
        component: resolve => require(['@views/error'], resolve), // vue-router配置路由 使用vue的异步组件技术,实现按需加载,一个组件生成一个js文件
        meta: {title:'少女虫拍客空间',keepAlive: false},
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
      document.title = to.meta.title
    }
    console.log(to)
    console.log(from)
    if(to.path == '/index'){
        // 跳转的是登陆
        next()
    }else{
        // 跳转的不是登录
        // 判断是否有token
        // let token = store.state.token;
        let token = localStorage.getItem('token');
        console.log(token)
        if(token){
            next()
        }else{
            next({
                path: '/index',
            })
        }
    }
    
})

export default router
